package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.LNew;
import com.learn.core.domain.LUser;
import com.learn.core.domain.view.UserDetailsGather;
import com.learn.core.dto.LNewDTO;
import com.learn.core.service.ILNewService;
import com.learn.core.service.ILUserService;
import com.learn.core.service.IUserDetailsGatherService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LNewController
 * @date 2021.05.31 09:28
 */
@Api("新闻")
@RestController
@RequestMapping("/manager/new")
public class LNewController extends WebController<LNew> {
    @Autowired
    ILNewService ilNewService;
    @Autowired
    IUserDetailsGatherService iUserDetailsGatherService;

    //竞赛模块列表
    @ApiOperation(value = "新闻列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<LNew> list(@RequestBody LNewDTO lNewDTO){
        try {
            if(lNewDTO==null||lNewDTO.getPage()==null||lNewDTO.getRows()==null){
                return new PageResult<LNew>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lNewDTO.getPage(),lNewDTO.getRows());
            QueryWrapper<LNew> queryWrapper = new QueryWrapper<LNew>();
            if (StringUtils.isNotEmpty(lNewDTO.getName()))
                queryWrapper.like("new_title",lNewDTO.getName());
            queryWrapper.orderByDesc("new_top","release_time");
            Page<LNew> lNews = (Page<LNew>) ilNewService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lNews)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (LNew lNew: lNews) {
                QueryWrapper<UserDetailsGather> gatherQueryWrapper = new QueryWrapper<UserDetailsGather>();
                gatherQueryWrapper.like("user_id",lNew.getCreateUser());
                UserDetailsGather userDetailsGather = iUserDetailsGatherService.getOne(gatherQueryWrapper);
                if(null != userDetailsGather)
                    lNew.setUserName(userDetailsGather.getName());
            }
            // 返回
            return new PageResult(lNews.getTotal(), lNews, ResultEnum.SUCCESS);
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新新闻", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LNew lNew, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lNew!=null){
            lNew.setUpdateTime(new Date());
            lNew.setUpdateUser(userId);
            if(ilNewService.updateById(lNew))
                return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加新闻", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LNew lNew, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lNew!=null) {
                if (StringUtils.isEmpty(lNew.getNewTitle()))
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_NEW_TITLE);
                if (StringUtils.isEmpty(lNew.getNewText()))
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_NEW_TEXT);
                lNew.setCreateTime(new Date());
                lNew.setCreateUser(userId);
                lNew.setReleaseState("0");
                if (StringUtils.isEmpty(lNew.getNewTop()))
                    lNew.setNewTop(0);
                if (ilNewService.save(lNew))
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除新闻", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LNew lNew){
        try {
            if(lNew!=null){
                if(lNew.getNewId()!=null){
                    QueryWrapper<LNew> queryWrapper = new QueryWrapper<LNew>();
                    queryWrapper.in(true, "new_id", lNew.getNewId());
                    if(ilNewService.remove(queryWrapper))
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //发布
    @ApiOperation(value = "发布")
    @GetMapping("/release")
    @UserLoginToken
    @Log(title = "发布新闻", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> release(@RequestParam("id")Integer id,@RequestParam("release")String release, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(id!=null){
            LNew lNew = new LNew();
            lNew.setNewId(id);
            lNew.setReleaseState(release);
            lNew.setReleaseTime(new Date());
            lNew.setUpdateTime(new Date());
            lNew.setUpdateUser(userId);
            if(ilNewService.updateById(lNew))
                return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}
